﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DiskFileUpload
{
    
    public class FileItem: System.IDisposable
    {
        public System.IO.Stream Stream { get; private set; }
        public string ContentType { get; private set; }
        public string Name { get; private set; }
        public string Value{ get; set;}
        
        
        public bool IsFormField { get; private set; }
        public string ClientFullPath { get; private set; }
        public string TempFullPath { get; private set; }
        
        // 构造函数
        public FileItem(string name, bool isForm, string path, string contentType)
        {
            this.Name = name;
            this.IsFormField = isForm;
            this.ClientFullPath = path;
            this.ContentType = contentType;

            if (!this.IsFormField)
            {
                // 创建临时文件
                this.TempFullPath = System.IO.Path.GetTempFileName();
                this.Stream = new System.IO.FileStream(
                    this.TempFullPath,
                    System.IO.FileMode.Create
                    );
            }
        }

        public string FileName
        {
            get
            {
                return System.IO.Path.GetFileName(this.ClientFullPath);
            }
        }

        public long Length
        {
            get
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(this.TempFullPath );
                return fi.Length;
            }
        }

        // 另存为
        public void SaveAs(string path)
        {
            System.IO.File.Copy(
                this.TempFullPath,
                path
                );
        }

        // 删除临时文件
        public void Delete()
        {
            if (System.IO.File.Exists(this.TempFullPath))
            {
                System.IO.File.Delete(this.TempFullPath);
            }
        }

        public string ToContentString(string boundary)
        {
            System.Text.StringBuilder builder = new StringBuilder();
            builder.AppendFormat("\r\nContent-Disposition: form-data; name=\"{0}\"", this.Name);
            if (this.IsFormField)
            {
                builder.Append("\r\n\r\n");
                builder.AppendFormat("{0}",this.Value);
            }
            else
            {
                builder.AppendFormat("; filename=\"{0}\"\r\n", this.ClientFullPath );
                builder.Append("Content-Type: application/octet-stream\r\n");
                builder.Append("\r\n");
            }
            builder.Append(boundary);
            return builder.ToString();
        }

        public void Finish()
        {
            if( this.Stream != null )
                this.Stream.Close();
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (this.Stream != null)
            {
                this.Stream.Dispose();
            }

            this.Delete();
        }

        #endregion
    }
}
